package com.leyou.user.controller;

import com.leyou.user.dto.AddInfoDTO;
import com.leyou.user.dto.AddressDTO;
import com.leyou.user.dto.UserAddressDTO;
import com.leyou.user.entity.Area;
import com.leyou.user.entity.City;
import com.leyou.user.entity.Province;
import com.leyou.user.entity.UserAddress;
import com.leyou.user.service.AddressService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired
    private AddressService addressService;
    /**
     * 根据
     * @param userId 用户id
     * @param id 地址id
     * @return 地址信息
     */
    @GetMapping
    public ResponseEntity<AddressDTO> queryAddressById(@RequestParam("userId") Long userId, @RequestParam("id") Long id){
        AddressDTO address = new AddressDTO();
        address.setId(1L);
        address.setStreet("珠吉路58号津安创业园一层黑马程序员");
        address.setCity("广州");
        address.setDistrict("天河区");
        address.setAddressee("小飞飞");
        address.setPhone("15800000000");
        address.setProvince("广东");
        address.setPostcode("510000");
        address.setIsDefault(true);
        return ResponseEntity.ok(address);
    }

    /**
     * 加载所有省份
     * @param pid 省份id
     * @param cid 市级id
     * @return
     */
    @GetMapping("/loadAddrInfo")
    public ResponseEntity<AddInfoDTO> findAdds(@RequestParam("pid")String pid, @RequestParam("cid")String cid){
        AddInfoDTO addInfoDTO = addressService.findAdds(pid,cid);
        return ResponseEntity.ok(addInfoDTO);
    }

    /**
     * 当省级改变时加载市级
     * @param pid 省份id
     * @return
     */
    @GetMapping("/loadCity")
    public ResponseEntity<List<City>> loadCity(@RequestParam("pid")String pid){
        List<City> cityList = addressService.loadCity(pid);
        return ResponseEntity.ok(cityList);
    }

    /**
     * 当市级改变时加载区级
     * @param cid 市级id
     * @return
     */
    @GetMapping("/loadArea")
    public ResponseEntity<List<Area>> loadArea(@RequestParam("cid")String cid){
        List<Area> areaList = addressService.loadArea(cid);
        return ResponseEntity.ok(areaList);
    }

    /**
     *  新增或修改收货地址
     * @param userAddressDTO 收货地址信息
     * @return
     */
    @PostMapping("/addUserAddress")
    public ResponseEntity<Void> addUserAddress(HttpServletRequest request, @RequestBody UserAddressDTO userAddressDTO){
        addressService.addUserAddress(request,userAddressDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 查询所有收货地址
     * @return
     */
    @GetMapping("/loadAddrList")
    public ResponseEntity<List<UserAddressDTO>> loadAddrList(HttpServletRequest request){
        List<UserAddressDTO> userAddressDTOS = addressService.loadAddrList(request);
        return ResponseEntity.ok(userAddressDTOS);
    }

    /**
     * 删除地址
     * @param id 地址id
     * @return
     */
    @DeleteMapping("/delReceive/{id}")
    public ResponseEntity<Void> delReceive(HttpServletRequest request,@PathVariable("id")String id){
        addressService.delReceive(request,id);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改为默认地址
     * @param id 地址id
     * @return
     */
    @PutMapping("/setDefault/{id}")
    public ResponseEntity<Void> setDefault(HttpServletRequest request,@PathVariable("id")String id){
        addressService.setDefault(request,id);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }


}